<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Db;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
        $this->brand=model('admin/Brand');
        $this->homestatistics=model('admin/kuozhan/Homestatistics');
        $this->friend=model('admin/kuozhan/Friend');
        $this->studaying=model('admin/kuozhan/Studaying');
        $this->article=model('admin/kuozhan/Articles');
        $this->area=model('admin/Area');
        $this->jihua=model('admin/kuozhan/Jihua');

    }

    /**
     * 首页基础信息
     *
     */
    public function index()
    {
        $data['top_logo']=Config('site.top_logo');
        $data['bottom_logo']=Config('site.bottom_logo');
        $data['contact_imge']=Config('site.contact_imge');
        $data['wechat']=Config('site.wechat');
        $data['erweima']=Config('site.erweima');

        $data['bottom_images']=$this->friend->where('type','底部图片')->order('weigh desc')->select();
        $data['zizhi_images']=$this->friend->where('type','资质照片')->order('weigh desc')->select();;
        $data['kefu']=Config('site.kefu');
        $data['search_keywords']=Config('site.search_keywords');
        $data['goods_teacher']=Config('site.goods_teacher');
        $data['free_course']=Config('site.free_course');
        $data['teacher_shouyi']=Config('site.teacher_shouyi');

        $data['score_bi']=Config('site.score_bi')/100;

        $data['home_msg']=explode("#",Config('site.home_msg'));
       
       $this->success('OK',$data);
    }
    /**
     * 轮播图
     */
    public function brandList($type=null){
        $where=[];
        if($type!=null){
            $where['type']=['=',$type];
        }
        $this->success('ok',$this->brand->where($where)->select());
    }

    /**
     * 首页系统统计
     */
    public function homestatistics(){

       $this->success('ok',$this->homestatistics->order('weigh asc')->select());
    }

    /**
     * 友情链接
     */
    public function friend($type='友情链接'){
         $where['type']=['=','友情链接'];
        $this->success('OK',$this->friend->where($where)->order('weigh desc')->select());

    }
    /**
     * 学习中心
     */
    public function studaying(){
        $this->success('OK',$this->studaying->order('weigh desc')->select());

    }

    /**
     * 栏目文章详情
     */
    public function article($type){
        $where['type']=['=',$type];
        $this->success('OK',$this->article->where($where)->find());

    }

    public function areaList($pid){
     $this->success('OK',$this->area->where('pid',$pid)->select())   ;

    }

    public function jihua($pege=1,$limit=10){
      $jihuaList=  $this->jihua->order('weigh desc') ->paginate($limit);
    foreach ($jihuaList as $k=>$v){
        $jihuaList[$k]['end_time_text']=date('m月d日 H:i',$v['end_time']);

    }
      $this->success('OK',$jihuaList);
    }

    public function  userList(){
        $userList=Db::name('user')->select();
        foreach ($userList as $key=>$value){
            $juse=0;
            if(Db::name('user_student')->where('user_id',$value['id'])->find()){
                $juse=1;
            }
            if(Db::name('user_teacher')->where('user_id',$value['id'])->find()){
                $juse=2;
            }
            if(Db::name('user_study')->where('user_id',$value['id'])->find()){
                $juse=4;
            }
            if(Db::name('user_check')->where('user_id',$value['id'])->find()){
                $juse=3;
            }
            if(Db::name('user_enterprise')->where('user_id',$value['id'])->find()){
                $juse=5;
            }
            Db::name('user')->where('id',$value['id'])->update(['juese'=>$juse]);

        }

    }


//put_world(time(),$a);

        public function category(){
               $list=Db::name('mechanism_category')->where('pid',0)->select();
               foreach ($list as $key=>$value){
               Db::name('mechanism_category')->where('id',$value['id'])->update(['level'=>1]);

                  $list1=Db::name('mechanism_category')->where('pid',$value['id'])->select();
                  foreach ($list1 as $k=>$v){
                      Db::name('mechanism_category')->where('id',$v['id'])->update(['level'=>2]);
                      Db::name('mechanism_category')->where('pid',$v['id'])->update(['level'=>3]);

                  }

               }
        }

}
